//index.js
import api from "../../utils/api.js";
import Toast from '@vant/weapp/toast/toast';
const app = getApp();

Page({
  data: {
    managerName: null,
    managerPassword: null,
    rand_code: null,
    code: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ value: null })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  setAccountValue: function (event) {
    this.setData({ managerName: event.detail })
  },

  setPassWordValue: function (event) {
    this.setData({ managerPassword: event.detail })
  },
  setRandCode: function (event) {
    this.setData({ rand_code: event.detail })
  },

  getCode: function (event) {
    var timeStamp = Date.parse(new Date());
    timeStamp = timeStamp / 1000;
    // this.setData({ code: api.code + `?t=${timeStamp}` })
    const url = api.code + `?t=${timeStamp}`
    const _this = this
    wx.request({
      url: url,
      method: "GET",
      data: {},
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        _this.setData({ code: res.data.data })
        var cookie = res.header["Set-Cookie"];
        if (cookie != null) {
          wx.setStorageSync("sessionid", res.header["Set-Cookie"]);//服务器返回的 Set-Cookie，保存到本地
        }
      },
      fail(res) {
        //更改备注失败回调函数
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  },

  onSubmit: function (event) {
    const { managerName, managerPassword, rand_code } = this.data
    if (!managerName) {
      return Toast({
        message: "登录账号不能为空",
        duration: 1500
      });
    } else if (!managerPassword) {
      return Toast({
        message: "密码不能为空",
        duration: 1500
      });
    } else if (!rand_code) {
      return Toast({
        message: "验证码不能为空",
        duration: 1500
      });
    }
    const _that = this
    var header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取本地保存好的上⼀次cookie
      "Content-Type": "application/x-www-form-urlencoded",
    };
    wx.request({
      url: api.login,
      method: "POST",
      data: { managerName, managerPassword, rand_code },
      header: header,
      success(res) {
        //更改备注成功回调函数
        if (res.data.code == 200) {
          Toast({
            message: "登录成功",
            duration: 1500
          });
          wx.redirectTo({
            url: '/pages/issues/index',
          });
        } else {
          Toast({
            message: "登录失败",
            duration: 1500
          });
        }
      },
      fail(res) {
        //更改备注失败回调函数
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none'
        });
      }
    });
  },

});
